package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.Customer;
import service.CustomerManager;
import service.impl.CustomerManagerFactory;

/**
 * Servlet implementation class AddCustomerServlet
 */
public class AddCustomerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private CustomerManager manager;   
    
    public AddCustomerServlet() {
        super();
        manager=CustomerManagerFactory.getInstance();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接参
		String cname=request.getParameter("cname");
		String csex=request.getParameter("sex");
		String csal=request.getParameter("sal");
		String cbirth=request.getParameter("birth");
		String[] favs=request.getParameterValues("favs");	
		System.out.println(favs.toString());
		Connection con=(Connection)request.getServletContext().getAttribute("connection");
		
		//较验
		
		//类型转换
		Date birth=null;
		boolean sex=Boolean.parseBoolean(csex);
		double sal=Double.parseDouble(csal);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
			birth=sdf.parse(cbirth);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		//封装
		Customer c=new Customer(null, cname, sal, sex, birth, favs);
		//调用业务
 
		int m=manager.addCustomer(c,con);
		if(m>0){
			//存入Context
			//request.setAttribute("c", c);
			//转发
			response.sendRedirect("list");
		}
		else{
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
	}

}














